package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.lq.shopping.dao.ShopGoodstypeDao;
import org.lq.shopping.dao.impl.ShopGoodstypeDaoImpl;
import org.lq.shopping.entity.ShopGoodstype;
import org.lq.shopping.service.ShopGoodstypeService;

public class ShopGoodstypeServiceImpl implements ShopGoodstypeService {
	
	ShopGoodstypeDao shopgoodstypedao = new ShopGoodstypeDaoImpl();

	@Override
	public boolean addShopGoodsType(ShopGoodstype gtype) {
		return shopgoodstypedao.add(gtype)>0;
	}

	@Override
	public boolean deleteGoodsType(int id) {
		return shopgoodstypedao.delete(id)>0;
	}

	@Override
	public boolean updateGoodsType(ShopGoodstype gtype) {
		return shopgoodstypedao.update(gtype)>0;
	}

	@Override
	public List<ShopGoodstype> findAllGoodstype() {
		List<ShopGoodstype> parent = new ArrayList<>();
		List<ShopGoodstype> shopgoodstypes = shopgoodstypedao.findAll();
		for (ShopGoodstype shopGoodstype : shopgoodstypes) {
			if(shopGoodstype.getParent_id()==0){
				parent.add(shopGoodstype);
				for (ShopGoodstype child : shopgoodstypes) {
					if(child.getParent_id()==shopGoodstype.getGoodstype_id()){
						shopGoodstype.getChilds().add(child);
					}
				}
			}
		}
		return parent;
	}

	@Override
	public List<ShopGoodstype> findShopGoodstypebyParentid(int parentId) {
		return shopgoodstypedao.findShopGoodstypebyParentid(parentId);
	}

	@Override
	public ShopGoodstype getShopGoodsbychildId(int goodstypeid) {
		return shopgoodstypedao.getShopGoodsbychildId(goodstypeid);
	}

}
